package com.example.service.impl;

import com.example.mapper.ClazzMapper;
import com.example.pojo.Clazz;
import com.example.pojo.ClazzQueryParam;
import com.example.pojo.PageResult;
import com.example.service.ClazzService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceimpl implements ClazzService {


    @Autowired
    private ClazzMapper clazzMapper;


    //删除班级信息
    @Override
    public void delete(Integer id) {
        clazzMapper.delete();
    }

    //添加班级信息
    @Override
    public void insert(Clazz clazz) {
        //补全基础属性
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        //插入数据
        clazzMapper.insert(clazz);
    }

    //根据id查询班级
    @Override
    public Clazz getInfoByID(Integer id) {
        return clazzMapper.getInfoByID(id);
    }

    //修改班级信息
    @Override
    public void update(Clazz clazz) {
        //更新修改时间
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.update(clazz);
    }


    //条件查询班级
    @Override
    public PageResult<Clazz> list(ClazzQueryParam clazzQueryParam) {
        PageHelper.startPage(clazzQueryParam.getPage(), clazzQueryParam.getPageSize());

        List<Clazz> clazzList = clazzMapper.list(clazzQueryParam);

        Page<Clazz> p =(Page<Clazz>) clazzList;

        return  new PageResult<Clazz>(p.getTotal(), clazzList);
    }
}
